package com.guangzhi.weijianzhi.company.manager;

import java.util.ArrayList;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.guangzhi.weijianzhi.R;
import com.guangzhi.weijianzhi.activity.BaseActivity;
import com.guangzhi.weijianzhi.adapter.MyArrayApterOne;
import com.guangzhi.weijianzhi.entity.PublishPositionEntity;
import com.guangzhi.weijianzhi.utils.Misc;
import com.guangzhi.weijianzhi.utils.PopuUtils;

public class PublishPositionActivity extends BaseActivity implements OnClickListener {

	private TextView mType;
	private EditText mPosiName;
    private CloseReceiver closeReceiver;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tz_publish_positon_layout);
		initView();
		onregistBroab();
	}

	private void onregistBroab() {
		// TODO Auto-generated method stub
		closeReceiver = new CloseReceiver();  
		IntentFilter intentFilter = new IntentFilter("com.guangzhi.weijianzhi");  
		registerReceiver(closeReceiver, intentFilter);  
	}

	private void initView() {
		// TODO Auto-generated method stub
		initTitle();
		goBack.setOnClickListener(this);
		titleTextV.setText("发布职位");
		mPosiName = (EditText) findViewById(R.id.posi_name);
		findViewById(R.id.posi_type).setOnClickListener(this);
		mType = (TextView) findViewById(R.id.type);
		findViewById(R.id.next).setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.posi_type:
			showType();
			break;
		case R.id.next:
			if(isOk()){
				PublishPositionEntity.posiName =mPosiName.getText().toString();
				
				Intent intent = new Intent(this, PublishPositionTwoActivity.class);
				startActivity(intent);
			}
			
			break;
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(closeReceiver);
	}
	
	
	private void showType() {
		
		final ArrayList<String> arrays = new ArrayList<String>();
		arrays.add("传单派发");arrays.add("客服文员");
		arrays.add("安保人员");arrays.add("促销/导购");	arrays.add("钟点工");
		arrays.add("工人");arrays.add("服务员/店员");
		arrays.add("礼仪/模特");arrays.add("销售/业务");
		arrays.add("家教");arrays.add("翻译");
		arrays.add("问卷调查");arrays.add("其他");
		// TODO Auto-generated method stub
		View view = View.inflate(this, R.layout.tz_pop_list_layout, null);
		ListView list = (ListView) view.findViewById(R.id.one_list);
		final PopupWindow showPou = PopuUtils.showPou(View.inflate(this, R.layout.tz_company_info_layout, null), view, this, this);
		MyArrayApterOne array = new MyArrayApterOne(arrays, this);
		list.setAdapter(Misc.getMyadapter(array, list));
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				showPou.dismiss();
				mType.setText(arrays.get(arg2));
				
				switch (arg2) {
				case 0:
					PublishPositionEntity.posiType ="2";
					break;
				case 1:
					PublishPositionEntity.posiType ="24";
					break;
				case 2:
					PublishPositionEntity.posiType ="25";
					break;
				case 3:
					PublishPositionEntity.posiType ="1";
					break;
				case 4:
					PublishPositionEntity.posiType ="5";
					break;
				case 5:
					PublishPositionEntity.posiType ="26";
					break;
				case 6:
					PublishPositionEntity.posiType ="27";
					break;
				case 7:
					PublishPositionEntity.posiType ="15";
					break;
				case 8:
					PublishPositionEntity.posiType ="13";
					break;
				case 9:
					PublishPositionEntity.posiType ="10";
					break;
				case 10:
					PublishPositionEntity.posiType ="11";
					break;
				case 11:
					PublishPositionEntity.posiType ="3";
					break;
				case 12:
					PublishPositionEntity.posiType ="23";
					break;
				
				}
				
			}
		});
	}
	
	private boolean isOk(){
		
		if(!Misc.notNull(mPosiName.getText().toString())){
			Misc.alert("请输入职位名称");
			return false;
		}else if(mPosiName.getText().toString().length()>20){
				Misc.alert("职位名称最多20字");
				return false;
		}else if(!Misc.notNull(mType.getText().toString())){
			Misc.alert("请选择职位类型");
			return false;
		}else{
			return true;
		}
		
	}
	
	   /** 
	    * 结束Activity的广播接收机 
	    * @author shuxiang 
	    * 
	    */  
	   public class CloseReceiver extends BroadcastReceiver  
	{   
	       @Override  
	       public void onReceive(Context context, Intent intent)  
	       {  
	    	   PublishPositionActivity.this.finish();  
	       }  
	   }  
	
}
